package com.sxm.yiwei.fragments;


import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.sxm.yiwei.R;
import com.sxm.yiwei.activitis.OrderDispatchingActivity;
import com.sxm.yiwei.activitis.OrderInOverActivity;
import com.sxm.yiwei.activitis.OrderOverActivity;
import com.sxm.yiwei.activitis.OrderPickupActivity;
import com.sxm.yiwei.activitis.OrderSelectActivity;
import com.sxm.yiwei.utils.InternetAddressUtis;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * 我的接单
 * A simple {@link Fragment} subclass.
 *
 */

public class ShopOrderFragment extends Fragment implements View.OnClickListener{

    private RelativeLayout dispatching;
    private RelativeLayout inover;
    private RelativeLayout over;
    private RelativeLayout pickup;
    private RelativeLayout select;
    private SharedPreferences sharedPreferences;
    private String appId;
    private String userId;
    private String token;
    private TextView deliverytext;
    private TextView endtext;
    private TextView rechooestext;
    private TextView repickuptext;
    private TextView pickuptext;
    private final static int PEOPLE = 1000;
    public static int num;
    private Integer choose1;
    private Integer end1;
    private Integer rePickup1;
    private Integer pickup1;
    private Integer delivery1;

    public ShopOrderFragment() {

        // Required empty public constructor
    }
    @Override
    public void onStart() {
        super.onStart();

        getOrderNumber();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_shop_order, container, false);
        sharedPreferences = getActivity().getSharedPreferences("userlogin", Context.MODE_PRIVATE);
        appId = sharedPreferences.getString("appid", "0");
        userId = sharedPreferences.getString("userId", "1");
        token = sharedPreferences.getString("token", "2");

        initOrderView(view);
        getOrderNumber();
        return view;
    }

    public void getOrderNumber() {
        RequestParams params = new RequestParams(InternetAddressUtis.Url+InternetAddressUtis.RECIVERBOKK_NUMBER);
        params.addHeader("Content-Type","application/json");
        params.addHeader("token",token);
        params.addHeader("appId",appId);
        params.addHeader("userId",userId);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                com.alibaba.fastjson.JSONObject obj = JSON.parseObject(result);
                String data = obj.getString("data");
                com.alibaba.fastjson.JSONObject obj1 = JSON.parseObject(data);
                if (obj1!=null){
                    String reChoose = obj1.getString("reChoose");
                    String end = obj1.getString("end");
                    String rePickup = obj1.getString("rePickup");
                    String pickup = obj1.getString("pickup");
                    String delivery = obj1.getString("delivery");
                    if (reChoose==null){
                        rechooestext.setText("0");
                    }else {
                        rechooestext.setText(reChoose);
                    }
                    if (end==null){
                        endtext.setText("0");
                    }else {
                        endtext.setText(end);
                    }
                    if (rePickup==null){
                        repickuptext.setText("0");
                    }else {
                        repickuptext.setText(rePickup);
                    }
                    if (pickup==null){
                        pickuptext.setText("0");
                    }else {
                        pickuptext.setText(pickup);
                    }
                    if (delivery==null){
                        deliverytext.setText("0");
                    }else {
                        deliverytext.setText(delivery);
                    }

                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }
    private void initOrderView(View view) {
        dispatching = (RelativeLayout) view.findViewById(R.id.order_dispatching);
        inover = (RelativeLayout) view.findViewById(R.id.order_inover);
        over = (RelativeLayout) view.findViewById(R.id.order_over);
        pickup = (RelativeLayout) view.findViewById(R.id.order_pickup);
        select = (RelativeLayout) view.findViewById(R.id.order_select);
        deliverytext = (TextView) view.findViewById(R.id.order_deliverytext);
        endtext = (TextView) view.findViewById(R.id.order_endtext);
        rechooestext = (TextView) view.findViewById(R.id.order_rechoosetext);
        repickuptext = (TextView) view.findViewById(R.id.order_repickuptext);
        pickuptext = (TextView) view.findViewById(R.id.order_pickuptext);

        select.setOnClickListener(this);
        over.setOnClickListener(this);
        inover.setOnClickListener(this);
        pickup.setOnClickListener(this);
        dispatching.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.order_select:
                startActivity(new Intent(getActivity(), OrderSelectActivity.class));
                break;
            case R.id.order_inover:
                startActivity(new Intent(getActivity(), OrderInOverActivity.class));
                break;
            case R.id.order_pickup:
                startActivity(new Intent(getActivity(), OrderPickupActivity.class));
                break;
            case R.id.order_over:
                startActivity(new Intent(getActivity(), OrderOverActivity.class));
                break;
            case R.id.order_dispatching:
                startActivity(new Intent(getActivity(), OrderDispatchingActivity.class));
                break;
        }
    }
}
